<?php
/**
 * 《邮箱管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: LogAction.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */

require_once(dirname(__FILE__) . '/MailUtil.class.php');

class Mail extends MailUtil {

	public $groupby = array(
		'-' => '请选择',
		'ip' => 'IP',
		'mac' => 'MAC',
		'2' => '日期',
		'3' => '月份',
	);

	public $orderby = array(
		'-' => '请选择',
		'ip' => 'IP',
		'mac' => 'MAC',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/03/06 11:29:07 $';
		$request['revision'] = '$Revision: 1.2 $';

		parent::__construct($request);
	}
	
	function tab($controller, $request) {		
		$data = array(
			'ARP信息' => array('cc'=>'log.php?do=Arp.find', 'js'=>''),
			'网络流量' => array('cc'=>'log.php?do=Network.find', 'js'=>''),
			'上网日志' => array('cc'=>'log.php?do=Layer7.find', 'js'=>''),
			'电脑开机' => array('cc'=>'log.php?do=ArpLog.find', 'js'=>''),
			'IP分配' => array('cc'=>'emp.php?do=Mac.find', 'js'=>''),	
			
			'内网地址' => array('cc'=>'info.php?do=Ip.find', 'js'=>''),
			'外网地址' => array('cc'=>'info.php?do=Qq.find', 'js'=>''),
		
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}
	
	function layout($controller, $request) {
		$this->pretreat($controller);
			
		// $controller->assign('t1', strtotime('-1 week'));
		// $controller->assign('t2', strtotime('+1 day'));
		
		require_once 'IMAP.connection_wizard.php'; 
		// Attempt to find the correct URI for any protocol based on common 
		// port settings and configurations 
		
		if (FALSE != ($url = Mail_IMAP_connection_wizard('mail.tecmold.com', 'sunxy@tecmold.com', 'jeaf', 'imap'))) { 
			echo $uri; 
		} else { 
			echo 'A suitable URI could not be detected.'; 
		}
		 
		// Attempt to find the correct URI on a specific protocol and port setting // 
		// Mail_IMAP_connection_wizard('mail.yourserver.net', 'mailuser', 'mailpass', 'imap', 143);
		

		$controller->display($request, 'mail/layout');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
			
		// $controller->assign('t1', strtotime('-1 week'));
		// $controller->assign('t2', strtotime('+1 day'));

		$controller->display($request, 'log/arp/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$mac_s = $request['mac_s'];
		$ip_s = $request['ip_s'];

		$where = " WHERE 1=1";
		if (!empty($mac_s)) {
			$where .= " AND mac = '" . $mac_s . "'";
		}

		if (!empty($ip_s)) {
			$where .= " AND ip like '" . $ip_s . "%'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);
		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'log/arp/list');
		return true;
	}
	
	function add($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];		
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		$controller->assign_by_ref('ip_option', $this->_ip($row['ip']));

		$controller->display($request, 'log/arp/add');
		return true;
	}
	
	function _ip($ip) {
		require_once(dirname(__FILE__) . '/../emp/Mac.class.php');
		$aa = new Mac();
				
		return $aa->ip($ip); 
	}
	
	function addPost($controller, $request) {

		$this->_add($request);
		
		$this->findPost($controller, $request);
		return true;
	}
	
	function _add($request) {
		require_once(dirname(__FILE__) . '/../emp/Mac.class.php');
		$aa = new Mac();
				
		return $aa->_add($request); 
	}
	
	function view($controller, $request) {
		$this->pretreat($controller);

		$row = $this->_view($request['id']);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'log/arp/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row = $this->_view_t($row);
		
		$row['mac_x'] = $this->_emp_mac($row['mac']);
		
		$row['ip_x'] = $this->_emp_ip($row['ip']);

		return $row;
	}
	
	function _emp_mac($mac) {
		require_once(dirname(__FILE__) . '/../emp/Mac.class.php');
		$aa = new Mac();
		
		return $aa->_view_mac($mac);
	}
	
	function _emp_ip($ip) {
		require_once(dirname(__FILE__) . '/../emp/Mac.class.php');
		$aa = new Mac();
		
		return $aa->_view_ip($ip);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		$controller->assign('t1', strtotime('-7 day'));
		$controller->assign('t2', strtotime('+1 day'));

		$controller->display($request, 'log/action/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		}

		if ($px == 'ip') {
			$row['a'] = $this->name_ip($row['a']);
		}
		
		$row = $this->_name_t($row, $px);

		return $row;
	}

	function name_ip($ip) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE ip = '%s'";
		$sql = sprintf($fmt, Constant::tbl_emp_mac, $ip);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			return sprintf("%s[%s]", Name::emp($row['emp']), $ip);
		}

		return $ip;
	}
	
	function t($controller, $request) {

		$file = "/home/sunxy/arp.txt";
		$fd = fopen($file, "r");
		while (!feof($fd)) {
			$buf = fgets($fd);
			// echo $buf;

			$this->_insert($buf);
		}
		fclose($fd);

		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}
	
	function _insert($buf) {
		if (strlen($buf) < 5) {
			return false;
		}
		
		$a = preg_split("/[\\s,]+/", $buf);
		if (count($a) < 5) {
			return false;
		}
		
		// print_r($a);

		$mac = strtoupper($a[2]);
		$ip = $a[0];
		
		if (!preg_match("/^(\d+)/", $ip)) {
			return false;
		}

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE mac = '%s' AND ip = '%s'";
		$sql = sprintf($fmt, $this->tbl, $mac, $ip);
		echo $sql . "\n";
		$result = $db->query($sql);
// 		if ($db->num_rows($result) == 0) {
		if ($row = $db->fetch_array($result)) {
			$fmt = "UPDATE %s SET modifytime = now() WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		} else {
			$fmt = "INSERT INTO %s(mac, ip, createtime, modifytime) VALUES('%s', '%s', now(), now())";
			$sql = sprintf($fmt, $this->tbl, $mac, $ip);
			echo $sql . "\n";
			$db->query($sql);
		}
		
		return false;
	}
	
	function nsz($controller, $request) {		
		$cmd = "/sbin/arp -n";		
		$s = shell_exec($cmd);
		$a = preg_split("/[\\n,]+/", $s);
		
		$i = 0;
		while (list($k, $buf) = each($a)) {
			if ($this->_insert($buf)) {
				$i++;
			}
		}
		
		if ($i > 0) {
			$request['a'] = '115';
			$request['ip'] = "192.168.2.%";
			$request['title'] = 'IP地址分配情况（南山）';
			
			$this->tjz($controller, $request);
		}
		
		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}
	
	function sjz($controller, $request) {		
		$cmd = "/sbin/arp -n";		
		$s = shell_exec($cmd);
		$a = preg_split("/[\\n,]+/", $s);
		
		$i = 0;
		while (list($k, $buf) = each($a)) {
			if ($this->_insert($buf)) {
				$i++;
			}
		}
		
		if ($i > 0) {
			$request['a'] = '116';
			$request['ip'] = "192.168.3.%";
			$request['title'] = 'IP地址分配情况（沙井）';
			
			$this->tjz($controller, $request);
		}
		
		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}

	function ns($controller, $request) {
		$request['a'] = '115';
		$request['ip'] = "192.168.2.%";
		$request['title'] = 'IP地址分配情况（南山）';
		
		$this->tj($controller, $request);
	}
	
	function sj($controller, $request) {
		$request['a'] = '116';
		$request['ip'] = "192.168.3.%";
		$request['title'] = 'IP地址分配情况（沙井）';
		
		$this->tj($controller, $request);
	}
	
	function tjz($controller, $request) {
		$bb = $this->_list_email($request['a']);
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			// echo "222";
			return false;
		}

		$from = $this->email;
		$subject = $request['title'];
		$ip = $request['ip'];
		
		$cc = array(	
			'新增信息'=>array('w' => "ip LIKE '$ip' AND DATE_FORMAT(createtime, '%Y-%m-%d') = DATE_FORMAT(now(), '%Y-%m-%d')", 'b'=>'id DESC'),
			// '更新信息'=>array('w' => "DATE_FORMAT(modifytime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d')", 'b'=>'id DESC'),			
		);
		
		if ($this->_tj_cc($controller, $cc)) {
			echo "222";
			return;
		}

		$body = $this->_tj_html($controller, $request, 'log/arp/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function tj($controller, $request) {
		$bb = $this->_list_email($request['a']);
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			// echo "222";
			return false;
		}

		$from = $this->email;
		$subject = $request['title'];
		$ip = $request['ip'];
		
		$cc = array(	
			'新增信息'=>array('w' => "ip LIKE '$ip' AND DATE_FORMAT(createtime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')", 'b'=>'id DESC'),
			// '更新信息'=>array('w' => "DATE_FORMAT(modifytime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d')", 'b'=>'id DESC'),			
		);
		
		if ($this->_tj_cc($controller, $cc)) {
			echo "222";
			return;
		}

		$body = $this->_tj_html($controller, $request, 'log/arp/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
}
?>
